package com.work.libtest2.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.work.libtest2.dto.DepartmentDTO;
import com.work.libtest2.entity.Department;
import com.work.libtest2.entity.Employee;
import com.work.libtest2.mapper.DepartmentMapper;
import com.work.libtest2.mapper.EmployeeMapper;
import com.work.libtest2.service.DepartmentService;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ClassName: DeptServiceImpl
 * Package: com.work.libtest2.service.impl
 * Description:
 *
 * @Author
 * @Create 2024/6/10 8:47
 * @Version 1.0
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper,Department> implements DepartmentService{

    @Resource
    private DepartmentMapper deptMapper;
    @Resource
    private EmployeeMapper employeeMapper;
    @Override
    public List<Department> getAll() {
        return deptMapper.selectList(null);
    }

    @Override
    public DepartmentDTO findDepByID(int id) {
        Department department = deptMapper.selectById(id);
        DepartmentDTO departmentDTO = new DepartmentDTO();
        BeanUtils.copyProperties(department,departmentDTO);
        if (department.getManagerId()!=null){
            Employee employee = employeeMapper.selectById(department.getManagerId());
            departmentDTO.setManagerName(employee.getEmpName());
        }
        return departmentDTO;
    }

    @Override
    public int DeleteDepById(Integer Dep_Id) {
        return deptMapper.deleteById(Dep_Id);
    }

    @Override
    public int SaveDepartment(Department department) {
        return deptMapper.insert(department);
    }

    @Override
    public int updateDep(Department department) {
        return deptMapper.updateById(department);
    }

    @Override
    public IPage<DepartmentDTO> getPage(Integer currentPage, Integer pageSize, Department department) {
        LambdaQueryWrapper<Department> lqw = new LambdaQueryWrapper<>();
        lqw.like(Strings.isNotEmpty(department.getDeptName()),Department::getDeptName,department.getDeptName());
        IPage page = new Page(currentPage,pageSize);
        deptMapper.selectPage(page,lqw);

        List<DepartmentDTO> list = new ArrayList<>();
        for (int i = 0; i < page.getRecords().size(); i++) {
            DepartmentDTO departmentDTO = new DepartmentDTO();
            Department department1 = (Department) page.getRecords().get(i);
            BeanUtils.copyProperties(department1,departmentDTO);
            if (department1.getManagerId()!=null){
                Employee employee = employeeMapper.selectById(department1.getManagerId());
                departmentDTO.setManagerName(employee.getEmpName());
            }
            list.add(departmentDTO);
        }
        page.setRecords(list);
        return page;
    }
}
